home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 524 / 524.xpi / chrome / mid.jar / content / midcontextmenu_ovr.js < prev    next >
Text File  |  2010-01-28  |  4KB  |  102 lines

  1. var midcontextmenu = {
  2.  
  3.   retries : 0, 
  4.   mid_prefsvc : Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService),
  5.   
  6.   onLoad: function() {
  7.     // initialization code
  8.     midcontextmenu.initialized = true;
  9.     midcontextmenu.strings = document.getElementById("midcontextmenu-strings");
  10.     document.getElementById("contentAreaContextMenu")
  11.             .addEventListener("popupshowing", function(e) { midcontextmenu.showContextMenu(e); }, false);
  12.   },
  13.  
  14.   showContextMenu: function(event) {
  15.  
  16.       var myitem = document.getElementById("context-midcontextmenu");
  17.       midcontextmenu.retries = 0;
  18.       if ((midcontextmenu.getTextSelected().length == 0) || !midcontextmenu.isContextEnabled()) {
  19.           myitem.hidden = true;
  20.       } else {
  21.           myitem.hidden = false;
  22.       }
  23.   },
  24.  
  25.   onMenuItemCommand: function(e) {
  26.  
  27.         function fibonacci(n) {
  28.             if (n==1 || n==2 ) {
  29.                return 1;
  30.             }
  31.             return fibonacci(n-1) + fibonacci(n-2);
  32.         }
  33.  
  34.         var selText = midcontextmenu.getTextSelected();
  35.  
  36.         const nsIWindowMediator = Components.interfaces.nsIWindowMediator;
  37.         var windowMediator = Components.classes['@mozilla.org/appshell/window-mediator;1'].getService();
  38.         windowMediator = windowMediator.QueryInterface(nsIWindowMediator);
  39.         var win = windowMediator.getMostRecentWindow("mid:main");
  40.  
  41.         //var b = getBrowser(); in a tab
  42.         //if (b) {
  43.         //   alert(b.contentWindow.location);
  44.         //   win = b.contentWindow;
  45.         //   win.domHelper.inputElement.value = selText;
  46.         //   win.mid_mud.lookup.lookupWord();
  47.         //   return;
  48.         //}
  49.  
  50.         if (!win || !win.domHelper) {
  51.             midcontextmenu.retries = midcontextmenu.retries + 1;
  52.             if (midcontextmenu.retries > 10) {
  53.                 midcontextmenu.retries = 0; 
  54.                 alert("mid not found :(");
  55.                 return;                            // couldn't start mid
  56.             } else { 
  57.                 if (midcontextmenu.retries == 1) {
  58.                     setTimeout(midtaskortoolsmenu.newMIDWindow,0);    // arguments? miniwiew, selection
  59.                 }
  60.             }
  61.             setTimeout(midcontextmenu.onMenuItemCommand,300 + fibonacci(midcontextmenu.retries)*100 )
  62.             return;
  63.         } else {
  64.             win.domHelper.inputElement.value = selText;
  65.             win.mid_mud.lookup.lookupWord();
  66.             win.focus();
  67.         }
  68.  
  69.         midcontextmenu.retries = 0;
  70.  
  71.   },
  72.  
  73.   getTextSelected: function() {
  74.  
  75.         var selection = "";
  76.         var node = document.popupNode;
  77.         var focusedWindow = new XPCNativeWrapper(document.commandDispatcher.focusedWindow, 'document', 'getSelection()');
  78.  
  79.         if ((node instanceof HTMLTextAreaElement) || ((node instanceof HTMLInputElement) && (node.type == "text")))  {
  80.              selection = node.value.substring(node.selectionStart, node.selectionEnd);
  81.         } else {
  82.           selection = focusedWindow.getSelection().toString();
  83.         }
  84.  
  85.    return selection;
  86.  
  87.   },
  88.  
  89.   isContextEnabled : function() {
  90.       if (midcontextmenu.mid_prefsvc.getPrefType("extensions.mid.menus.sendfromcontext") == midcontextmenu.mid_prefsvc.PREF_BOOL) {
  91.           if (midcontextmenu.mid_prefsvc.getBoolPref("extensions.mid.menus.sendfromcontext")) {
  92.               return true;
  93.           }
  94.       }
  95.       return false;
  96.   }
  97.  
  98.  
  99.  
  100. };
  101. window.addEventListener("load", function(e) { midcontextmenu.onLoad(e); }, false);
  102.